package com.lagou.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SortMapper extends Mapper<LongWritable, Text, IntWritable, NullWritable> {
    final IntWritable numberKey = new IntWritable();

    // LongWritable, Text-->文本偏移量，一行文本内容，map方法的输入参数，一行文本就调用一次map方法
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//        1 接收到文本内容，转为String类型
        final String str = value.toString();
//        2 按照空格进行切分
        final String[] nums = str.split(" ");
//        3 输出<单词，1>
        //遍历数据
        for (String n : nums) {
            //转换为int，使用默认的升序排序，更适合得到需要的输出结果
            numberKey.set(Integer.parseInt(n));
            context.write(numberKey, NullWritable.get());
        }

    }
}
